/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.sadhar.sipp.login;

import java.util.Map;
import org.sadhar.sia.framework.ClassApplicationModule;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.Session;
import org.zkoss.zk.ui.Sessions;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Window;

/**
 *
 * @author Hendro Steven
 */
public class LoginWnd extends ClassApplicationModule {

    private Textbox txtboxUserName;
    private Textbox txtboxPassword;
    private LoginDAO dao;

    public LoginWnd() {
        this.dao = new LoginDAOImpl();
    }

    public void onCreate() throws Exception {
        txtboxUserName = (Textbox) getFellow("txtboxUserName");
        txtboxPassword = (Textbox) getFellow("txtboxPassword");
    }

    public void btnLoginOnClick() throws Exception {
        if (txtboxUserName.getValue().length() != 9) {
            String kd = dao.getKodePegawai(txtboxUserName.getValue());//cari kdPegawai ditable personalia.pegawai
            if (kd.length() > 0) { //jika kdpegawai ada
                Map map = dao.getUserGroup(kd);
                if (map != null) {
                    Map login = dao.login(map.get("Tabel").toString(), txtboxUserName.getValue(), txtboxPassword.getValue(), true);
                    if (login != null) {
                        Session session = Sessions.getCurrent();
                        session.setAttribute("kdPegawai", kd);
                        final Window win = (Window) Executions.createComponents("/sippzul/login/TahunAnggaran.zul", this.getParent(), null);
                        win.doModal();
                    } else {
                        Messagebox.show("Password Salah", "Pesan Informasi", Messagebox.OK, Messagebox.INFORMATION);
                        return;
                    }
                } else {
                    Messagebox.show("User tidak berhak mengakses SI ini", "Pesan Informasi", Messagebox.OK, Messagebox.INFORMATION);
                    return;
                }
            } else {//jika kdpegawai tdk ada
                Messagebox.show("User Bukan Mahasiswa atau Pegawai USD", "Pesan Informasi", Messagebox.OK, Messagebox.INFORMATION);
                return;
            }
        } else {
            //login mahasiswa
        }
    }

    public void btnBatalOnClick() throws Exception {
        txtboxUserName.setValue("");
        txtboxPassword.setValue("");
        txtboxUserName.setFocus(true);
    }
}
